﻿using System;
using System.Configuration;
using System.Web;
using System.Web.UI;
using KPIS.GERP.ESS.APP.App_UI.Forms;

namespace KPIS.GERP.ESS.APP.App_UI.UserControls
{
    public partial class TaskMessage : UserControl
    {
        public string FrameZoneClientId
        {
            get
            {
                return frameZone.ClientID;
            }
        }

        public void LoadTaskMessage(int taskhistoryId, string sendId = null)
        {
            string strServer = HttpContext.Current.Request.Url.Host;
            string strUrlPrefix = string.IsNullOrEmpty(ConfigurationSettings.AppSettings["SubSystem"]) ? "" : "/" + ConfigurationSettings.AppSettings["SubSystem"];

            // ReqestPort = 80 mean production server, not 80 = debuging
            string serverPath = !HttpContext.Current.Request.Url.IsDefaultPort ? 
                "http://localhost:60830/" :
                string.Format("{2}://{0}{1}/WEB.GIMS/", strServer, strUrlPrefix,
                (Request.Url.AbsoluteUri.ToLower().StartsWith("https") ? "https" : "http"));
            string strTaskUrl = string.Format("../Task/Details?taskhistoryid={0}''NsendId={1}''NEmptyLayout=true", taskhistoryId, sendId);
            var sAuthen = new GerpAuthenService.GerpAuthenService();
            string tokenKey = sAuthen.CreateOTP(SsMng.EmpSeqI, true, strTaskUrl);
            string strUrl = string.Format("{0}Home/SingleSigon?&tokenKey={1}", serverPath, tokenKey);
            string strScript = string.Format("javascript:createFrame(\"{0}\"); ", strUrl);
            ScriptManager.RegisterStartupScript(Page, GetType(), "Javascript", strScript, true);
        }

        public void UnloadTaskMessage()
        {
            string strScript = "javascript:removeIframe();";
            ScriptManager.RegisterStartupScript(Page, GetType(), "Javascript", strScript, true);
        }
    }
}